package mac5855.app.controllers;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mac5855.app.dao.DAOFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Servlet implementation class Logout
 */
public class Logout extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DAOFactory daoFactory;
	
	
       
    @Override
	public void destroy() {
		this.daoFactory = null;
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
        DAOFactory.path );
    this.daoFactory = ( (DAOFactory) context.getBean( "daoFactory" ) );
		super.init();
	}

	/**
     * @see HttpServlet#HttpServlet()
     */
    public Logout() {
        
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = ((HttpServletRequest)request).getSession();
		
		session.removeAttribute("User");
		session.removeAttribute("UserType");
		
		session.invalidate();
		
		RequestDispatcher rd = request.getRequestDispatcher("/");
		rd.forward(request, response);
	}

}
